package com.wee.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wee.aircoach_user.Constant;
import com.wee.aircoach_user.PhysicalTestActivity;
import com.wee.aircoach_user.R;

public class tab_FragmentTest1 extends Fragment implements OnClickListener{
	/***tab1 点击开始  再次点击结束以此计算时间 然后的出结果*/
	//主要是想实现 开始按钮 然后 然后就记录 状态 通过下面的时间task计算时间 传递数最后传递的数据为value0

	View rootView;

	//开始
	private TextView start_txt,num_num;
	//完成下一个按钮
	private ImageButton btn;
	//重新测试
	private int number;
	private LinearLayout qw;
	private  int reclen;
	private int value0 = -1;
	private static int STATE_START = 0;
	private static int STATE_STOP = 1;
	private int state = STATE_STOP;
	//1代表测试完成可点击
	public static final int MSG_TIME_SRART = 1;
	public static final int MSG_TIME_RUNNGIN = 2;
	public static final int MSG_TIME_END = 3;
	public static final int MSG_TIME_READ = 4;

	private int timeValue = 0;
	private TimeTask task;
	private Handler mHandler = new Handler(){
		/**测试线程记录数据 timeValue 为记录次数 一秒加一
		 * 因为不是俯卧撑运动所以直接得到start_txt 数据*/
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			String text = "";
			if(msg.what == MSG_TIME_SRART){
				/***应为60s*/
				timeValue= Constant.scend1;
				text = timeValue +"";
			}else if(msg.what == MSG_TIME_END){
				/**d调用弹窗  得出最后的结果*/
				try{
					dialog();
					btn.setVisibility(View.VISIBLE);
				}catch (Exception e){}
			}else if(msg.what == MSG_TIME_RUNNGIN){
				num_num.setVisibility(View.VISIBLE);
				qw.setBackgroundResource(R.drawable.s_top);
				timeValue--;
				text = timeValue +"";
				num_num.setText(""+text);
				if (timeValue==1){
					task.stopTask();
				}
			}else if(msg.what ==MSG_TIME_READ){
				start_txt.setClickable(false);
				reclen--;
				num_num.setText(""+reclen);
				if(reclen==0){
					num_num.setVisibility(View.GONE);
					qw.setBackgroundResource(R.drawable.s_top);
				}
			}

		}
	};

	public interface CallBackToMain{
		public void callBackMain(boolean isTrue);
	}

	private CallBackToMain callBack;


	public CallBackToMain getCallBack() {
		return callBack;
	}

	public void setCallBack(CallBackToMain callBack) {
		this.callBack = callBack;
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.zice_fragment1, container, false);
		init();
		return rootView;
	}
	private void  dialog(){
		start_txt.setClickable(true);
		qw.setBackgroundResource(R.drawable.again);
		NumberPicker mPicker = new NumberPicker(getActivity());
		mPicker.setMinValue(0);
		mPicker.setValue(20);
		mPicker.setMaxValue(60);
		mPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				// TODO Auto-generated method stub
				num_num.setText(String.valueOf(newVal));
				number = Integer.parseInt(String.valueOf(newVal));
				updateTestValue(number);
			}
		});

		AlertDialog mAlertDialog = new AlertDialog.Builder(getActivity())
				.setTitle("体能测试").setView(mPicker).setPositiveButton("ok",null).create();
		mAlertDialog.show();
	}
	private void init() {
		ImageView imageView = (ImageView)rootView.findViewById(R.id.bg_1);
		ImageLoader.getInstance().displayImage("drawable://" + R.drawable.xinlvceshi, imageView);
		ImageView imageView1 = (ImageView)rootView.findViewById(R.id.bottom);
		ImageLoader.getInstance().displayImage("drawable://" + R.drawable.white_bottom, imageView1);
		//切换背景
		qw= (LinearLayout) rootView.findViewById(R.id.qw);
		//开始
		start_txt=(TextView)rootView.findViewById(R.id.btn_start);
		start_txt.setOnClickListener(this);
		//完成跳转
		btn=(ImageButton)rootView.findViewById(R.id.btn1);

		btn.setOnClickListener(this);
		num_num= (TextView) rootView.findViewById(R.id.num_num);

	}
	public class TimeTask implements Runnable{
		private Thread task;
		private boolean isStop = false;
		public TimeTask(){
			task = new Thread(this);
		}
		@Override
		public void run() {
			mHandler.sendEmptyMessage(MSG_TIME_SRART);
			while (!isStop){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if (reclen>0){
					mHandler.sendEmptyMessage(MSG_TIME_READ);
				}else{
					mHandler.sendEmptyMessage(MSG_TIME_RUNNGIN);
				}
			}
			mHandler.sendEmptyMessage(MSG_TIME_END);
		}
		public void start(){
			task.start();
		}

		public void stopTask(){
			isStop = true;
		}
	}

	private void updateTestValue(int value){
		value0 =value*3;
	}
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
			case R.id.btn1:
				try{
					if(callBack != null||value0!=-1){
						Bundle args=new Bundle();
						args.putInt("value0",value0);
						((PhysicalTestActivity)getActivity()).setFragmentArguments(args);
						callBack.callBackMain(true);
					}else{
						Toast.makeText(getActivity(), "请完成测试项目", Toast.LENGTH_SHORT).show();
					}
				}catch(Exception e){
				}
				break;
			case R.id.btn_start:
				start_txt.setClickable(false);
				try{
					reclen=4;
					qw.setBackgroundResource(R.drawable.beg_in);
					task = new TimeTask();
					task.start();
				}catch(Exception r){
				}
				break;
		}


	}


}
